package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.code.ResourceKind;
import com.pactera.jep.sys.model.Authorization;
import com.pactera.jep.sys.model.Menu;
import com.pactera.jep.sys.service.AuthorizationService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.code.WebHeaderCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统授权：菜单权限、功能权限和数据权限
 */

@Api(value = "系统授权：菜单权限、功能权限和数据权限")
@RestController
@RequestMapping("/authorization")
public class AuthorizationRestController extends BaseRestController<Authorization> {
	private static Logger logger = LoggerFactory.getLogger(AuthorizationRestController.class);

	@Autowired
	private AuthorizationService	authorizationService;

	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(@ApiIgnore @RequestHeader(WebHeaderCode.X_USER_TOKEN) String token, Page<Authorization> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
		authorizationService.query(page, filters, orders);
	}

    /**
     * 保存
     * @param json
     * @return
     */
    @ApiOperation(value = "保存菜单权限")
    @PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody JSONObject json) {
        Map<String, Object> tips = new HashMap<String, Object>();

        String nodes = (String)json.get("nodes");
        String code = (String)json.get("code");
        String type = (String)json.get("type");

		List<Menu> menuList = JSON.parseArray(nodes, Menu.class);

		try
		{
            authorizationService.save(menuList, code, type);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}

    /**
     *保存数据权限
     * @param json
     * @return
     */
    @ApiOperation(value = "保存数据权限")
    @PostMapping(value = "/savePermit")
	public Map<String, Object> savePermit(@RequestBody JSONObject json) {
		Map<String, Object> tips = new HashMap<String, Object>();

        String permitType = (String)json.get("permitType");
        String menuCode = (String)json.get("menuCode");
        String roleCode = (String)json.get("roleCode");
        String type = (String)json.get("type");
		try
		{
            /*添加新增的菜单权限*/
            Authorization authorization = new Authorization();
            authorization.setAuzTarget(roleCode);
            authorization.setAuzType(type);
            authorization.setKind(ResourceKind.PERMIT.name());
            authorization.setResourceCode(permitType);
            authorization.setMenuCode(menuCode);
            authorizationService.savePermit(authorization);

			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}

}